package com.fmsz.xmly.adapters.toplist;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fmsz.xmly.R;
import com.fmsz.xmly.activitys.classify.ClassifyFocusImageActivity;
import com.fmsz.xmly.entitys.toplist.DatasItemDataEntity;
import com.fmsz.xmly.entitys.toplist.FindTopListEntity;
import com.fmsz.xmly.entitys.toplist.FocusImagesItemEntity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/5.
 */

public class FindTopListAdapter extends RecyclerView.Adapter<FindTopListAdapter.ViewHolder> implements View.OnClickListener {

    private Context context;
    private List<DatasItemDataEntity> lists;
    private List<DatasItemDataEntity> lists2;
    private List<FocusImagesItemEntity> lists3;
    private static final String TAG = "FindTopListAdapter";
    private FindTopListEntity entity;

    public FindTopListAdapter(Context context) {
        this.context = context;
        lists = new ArrayList<>();
        lists2 = new ArrayList<>();
        lists3 = new ArrayList<>();
    }


    @Override
    public int getItemViewType(int position) {
        if (position==0) {
            return R.layout.find_toplist_head_item;
        }
        else if(position==1 || position==lists.size()+2){
            return R.layout.find_top_list_title_item;
        }
        return R.layout.find_toplist_normal_item;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(viewType, parent, false);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (getItemViewType(position)== R.layout.find_toplist_head_item) {
            String headUrl = lists3.get(position).getPic();
            Picasso.with(context).load(headUrl).into(holder.headImage);
        }else if(getItemViewType(position)==R.layout.find_top_list_title_item){
            if (position<=lists.size()) {
                holder.title.setText(entity.getDatas().get(0).getTitle());
            }else{
                holder.title.setText(entity.getDatas().get(1).getTitle());
            }
        }else if(getItemViewType(position)==R.layout.find_toplist_normal_item){
            if (position<=lists.size()+1) {
                String iconUrl = lists.get(position-2).getCoverPath();
                Picasso.with(context).load(iconUrl).into(holder.icon);
                holder.title_tv.setText(lists.get(position-2).getTitle());
                holder.first_tv.setText(lists.get(position-2).getFirstKResults().get(0).getTitle());
                holder.second_tv.setText(lists.get(position-2).getFirstKResults().get(1).getTitle());
            }else{
                String iconUrl = lists2.get(position-lists.size()-3).getCoverPath();
                Picasso.with(context).load(iconUrl).into(holder.icon);
                holder.first_tv.setText(lists2.get(position-lists.size()-3).getFirstKResults().get(0).getTitle());
                holder.second_tv.setText(lists2.get(position-lists.size()-3).getFirstKResults().get(1).getTitle());
            }
        }
    }

    @Override
    public int getItemCount() {
        return entity==null?lists.size()+lists2.size()+lists3.size():lists.size()+lists2.size()+lists3.size()+2;
    }

    public void addAll(FindTopListEntity entity){
        this.entity = entity;
        lists.addAll(entity.getDatas().get(0).getList());
        lists2.addAll(entity.getDatas().get(1).getList());
        lists3.addAll(entity.getFocusImages().getList());
        notifyItemRangeInserted(0,lists.size()+lists2.size()+lists3.size());
    }

    @Override
    public void onClick(View v) {
        if (v.findViewById(R.id.top_list_head_iv)!=null) {
            Intent intent = new Intent(context, ClassifyFocusImageActivity.class);
            intent.putExtra("htmlUrl",lists3.get(0).getUrl());
            context.startActivity(intent);
        }
    }



    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView headImage;
        private final TextView title;
        private final ImageView icon;
        private final TextView title_tv;
        private final TextView first_tv;
        private final TextView second_tv;

        public ViewHolder(View itemView) {
            super(itemView);
            headImage = ((ImageView) itemView.findViewById(R.id.top_list_head_iv));
            title = ((TextView) itemView.findViewById(R.id.find_top_list_title));
            icon = ((ImageView) itemView.findViewById(R.id.top_list_Icon));
            title_tv = ((TextView) itemView.findViewById(R.id.top_list_title));
            first_tv = ((TextView) itemView.findViewById(R.id.top_list_first_tv));
            second_tv = ((TextView) itemView.findViewById(R.id.top_list_second_tv));
        }
    }
}
